package com.letou.ls.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableFifFiveBottmBady extends TableLayout {
    
    private ArrayList<ArrayList<String>> mDataList;
    private int cellBorder = 1;
    private int tableTextColor = Color.BLACK;
    private int tableBackgroundAColor = Color.WHITE;
    private int tableBackgroundCColor = Color.parseColor("#ccffff");
    private int tableBackgroundDColor = Color.parseColor("#cacdd5");
    private Typeface tableTypeface = Typeface.SANS_SERIF;
    private int tableGravity = Gravity.CENTER;
    private float mLeafTextSize;
    private ArrayList<Float> mWidthList;
    private int iconId;
    private int iconNumber;
    private int[] mTypeList;
    
    public TableFifFiveBottmBady(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    public TableFifFiveBottmBady(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public TableFifFiveBottmBady(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
    }
    
    public void addData(ArrayList<ArrayList<String>> datalist, int[] typeList,
            ArrayList<Float> widthList, float textSize, int icon, int speed) {
        this.setBackgroundColor(Color.parseColor("#3962b4"));
        this.setBaselineAligned(true);
        
        this.removeAllViews();
        mDataList = datalist;
        mWidthList = widthList;
        mLeafTextSize = textSize;
        mTypeList = typeList;
        iconId = icon;
        iconNumber = speed;
        drawTableBody();
    }
    
    public void drawTableBody() {
        /**
         * 画表内容
         */
        if (mDataList != null) {
            int size = mDataList.size();
            int typeSize = 0;
            if (mTypeList != null) {
                typeSize = mTypeList.length;
            }
            TableRow allRow = new TableRow(this.getContext());
            TableLayout tableLayout1 = new TableLayout(this.getContext());
            TableLayout tableLayout2 = new TableLayout(this.getContext());
            if (iconNumber < mWidthList.size()) {
                
            } else {
                return;
            }
            for (int index = 0; index < size; index++) {
                TableRow row = new TableRow(this.getContext());
                
                row.setPadding(0, 1, 0, cellBorder);
                // int ss = mDataList.get(index).size();
                for (int j = 0; j < iconNumber; j++) {
                    
                    String text = mDataList.get(index).get(j);
                    int type = 0;
                    if (typeSize > 0 && j < typeSize) {
                        type = mTypeList[j];
                    }
                    
                    int width = 0;
                    if (mWidthList.size() > j) {
                        width = mWidthList.get(j).intValue();
                    }
                    if (width != 0) {
                        TableRow dataCell = getDellCell(text, type, width);
                        
                        row.addView(dataCell);
                    }
                    
                }
                tableLayout1.addView(row);
            }
            
            for (int index = 0; index < size; index++) {
                TableRow row = new TableRow(this.getContext());
                
                row.setPadding(0, 1, 0, cellBorder);
                int ss = mDataList.get(index).size();
                for (int j = iconNumber; j < ss; j++) {
                    
                    String text = mDataList.get(index).get(j);
                    int type = 0;
                    if (typeSize > 0 && j < typeSize) {
                        type = mTypeList[j];
                    }
                    
                    int width = 0;
                    if (mWidthList.size() > j) {
                        width = mWidthList.get(j + 1).intValue();
                    }
                    if (width != 0) {
                        TableRow dataCell = getDellCell(text, type, width);
                        
                        row.addView(dataCell);
                    }
                    
                }
                tableLayout2.addView(row);
            }
            
            ImageView imageView = new ImageView(this.getContext());
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(iconId);
            imageView.setBackgroundColor(Color.WHITE);
            TableRow.LayoutParams t = new TableRow.LayoutParams();
            t.setMargins(1, 0, 1, 0);
            t.width = mWidthList.get(iconNumber).intValue();
            t.height = TableRow.LayoutParams.MATCH_PARENT;
            imageView.setPadding(1, 0, 2, 0);
            imageView.setLayoutParams(t);
            imageView.setBackgroundColor(Color.RED);
            
            allRow.addView(tableLayout1);
            if (t.width != 0) {
                allRow.addView(imageView);
            }

            allRow.addView(tableLayout2);
            
            addView(allRow);
            
        }
    }
    
    private TableRow getDellCell(String text, int type, int width) {
        TextView dataCell = new TextView(this.getContext());
        TableRow itemRow = new TableRow(this.getContext());
        dataCell.setTextColor(tableTextColor);
        dataCell.setBackgroundColor(tableBackgroundAColor);
        
        dataCell.setTypeface(tableTypeface);
        dataCell.setGravity(tableGravity);
        // int height;
        dataCell.setTextSize(mLeafTextSize);
        dataCell.setText(text);
        if (width > 0) {
            dataCell.setWidth(width - 1);
        }
        switch (type) {
            case 0:
                // mBgPaint.setColor(Color.WHITE);
                break;
            case 1:
                itemRow.setBackgroundColor(Color.RED);
                break;
            case 2:
                itemRow.setBackgroundColor(Color.parseColor("#FF7F00"));
                // itemRow.setBackgroundColor(Color.YELLOW);
                break;
        }
        // switch (type) {
        // case 0:
        // break;
        // case 1:
        // dataCell.setTextColor(Color.RED);
        // dataCell.setBackgroundColor(tableBackgroundAColor);
        // break;
        // case 2:
        // dataCell.setTextColor(Color.RED);
        // dataCell.setBackgroundColor(tableBackgroundCColor);
        // break;
        // case 3:
        // dataCell.setTextColor(tableTextColor);
        // dataCell.setBackgroundColor(tableBackgroundCColor);
        // break;
        // case 4:
        // dataCell.setTextColor(tableTextColor);
        // dataCell.setBackgroundColor(tableBackgroundDColor);
        // break;
        // case 5:
        // dataCell.setTextColor(Color.RED);
        // dataCell.setBackgroundColor(tableBackgroundDColor);
        // break;
        // }
        
        TableRow.LayoutParams t = new TableRow.LayoutParams();
        t.setMargins(0, 0, 2, 0);
        dataCell.setLayoutParams(t);
        itemRow.addView(dataCell);
        return itemRow;
    }
    
}
